<?php 
class InterestsController extends SilkControllerBase {
	
	public function index($params) {
		$this->set("interests", orm("interest")->find_all(array("order" => "name ASC")));
		$createLink = SilkResponse::create_url(array(	"component" => "mp",
														"controller" => "interests",
														"action" => "create"
														));
		$this->set("createLink", $createLink);
		$deleteLink = SilkResponse::create_url(array(	"component" => "mp",
														"controller" => "interests",
														"action" => "delete"
														));
		$this->set("deleteLink", $deleteLink);
		$editLink = SilkResponse::create_url(array(	"component" => "mp",
													"controller" => "interests",
													"action" => "edit"
													));
		$this->set("editLink", $editLink);
	}
	
	public function create($params) {
		if(isset($params["input"])) {
			$interest = new Interest();
			$interest->fill_object_form($params);
			if($interest->save()) {
				$interestLink = SilkResponse::create_url(array(	"component" => "mp",
														"controller" => "interests",
														"action" => "index"
														));
				
				redirect($interestLink);
			} else {
				$this->set("error", "Error saving new Interest");
			}
		}
		$form = SilkForm::auto_form(orm("interest"), array());
		$this->set("form", $form);
	}
	
	public function delete($params) {
		if(isset($params["id"])) {
			$interest = orm("interest")->find_by_id($params["id"]);
			$interest->delete();
			
			$indexLink = SilkResponse::create_url(array("component" => "mp",
														"controller" => "interests",
														"action" => "index"
														));
			redirect($indexLink);
		}
	}
	
	public function edit($params) {
		$indexLink = SilkResponse::create_url(array("component" => "mp",
														"controller" => "interests",
														"action" => "index"
														));
		$this->set("indexLink", $indexLink);
		
		if(isset($params["input"])) {
			$interest = new Interest();
			$interest->fill_object_form($params);
			if( $interest->save() ) {
				redirect($indexLink);
			} else {
				$this->set("error", "Error saving changes to Interest");
				redirect($indexLink);
			}
		}
		
		if(isset($params["id"])) {
			$interest = orm("interest")->find_by_id($params["id"]);
			$editForm = SilkForm::auto_form($interest, array());
			$this->set("editForm", $editForm);
		}
	}
	
	public function showInterests( $params ) {
		$portfolio = MpController::get_portfolio( $params );
	}
}